package com.joysw.ui.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.joysw.R;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
	private Context context;
	List<String> groups;
	List<List<Map<String, String>>> childs;

	/*
	 * 构造函数: 参数1:context对象 参数2:一级列表数据源 参数3:二级列表数据源
	 */
	public ExpandableListAdapter(Context context, List<String> groups,
			List<List<Map<String, String>>> childs) {
		this.groups = groups;
		this.childs = childs;
		this.context = context;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childs.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	// 获取二级列表的View对象
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		@SuppressWarnings("unchecked")
		String text = ((Map<String, String>) getChild(groupPosition,
				childPosition)).get("child");
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		// 获取二级列表对应的布局文件, 并将其各元素设置相应的属性
		LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
				R.layout.expandlistview_child_item, null);
		TextView tv = (TextView) linearLayout.findViewById(R.id.childTo);
		tv.setText(text);

		return linearLayout;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childs.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	// 获取一级列表View对象
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		String text = groups.get(groupPosition);
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		// 获取一级列表布局文件,设置相应元素属性
		RelativeLayout linearLayout = (RelativeLayout) layoutInflater.inflate(
				R.layout.expandlistview_group_item, null);
		TextView textView = (TextView) linearLayout
				.findViewById(R.id.textView01);
		textView.setText(text);

		return linearLayout;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
